# 了解HER2切片的画图和切片功能代码
# 切块与展示图片代码

from src.acquisition.slice.patches import DigitalSlide, Patch, get_roi, get_seeds, draw_seeds
import src.acquisition.slice.patches.parameter as pr
from matplotlib import pyplot as plt

# 读取WSI的原图和标注，并进行一定的处理
def read_kfb(WSI_path,annotation_path,WSI_number):
    slide = DigitalSlide()
    tag = slide.open_slide(WSI_path, WSI_number)
    if tag:
        # 这里应该是指在GLOBAL_SCALE下，全图的长宽是多少
        ImageWidth, ImageHeight = slide.get_image_width_height_byScale(pr.GLOBAL_SCALE)
        # 根据上面获取到的长宽（相当于等比例缩小），生成一个fulllmage的信息（与直接open图片的格式是一样的）
        fullImage = slide.get_image_block(pr.GLOBAL_SCALE, 0, 0, ImageWidth, ImageHeight)
    # fullImage.save(r'I:\pyCharmProjectSet\deepLearning\otherWay\data\patches\test\fullImage.jpg') # 直接保存等比例缩小的全局图
    slide.read_annotation(annotation_path)
    # 根据标注创建标注区域
    # 根据标注区域
    mask_img = slide.create_mask_image(pr.GLOBAL_SCALE)

    return slide,fullImage,mask_img

# 实际切块的代码
def patches(slide,fullImage,mask_img,original_points_txt_path,result_points_txt_path,if_write_original_txt=False, if_result_original_txt=False):
    # 原来切割图片的代码
    # ex_patch = Patch()
    # ex_patch.get_roi_seeds(fullImage, pr.EXTRACT_PATCH_DIST)
    # # # # 这里EXTRACT_SCALE应该是放大倍数，PATCH_SIZE_HIGH应该是截出来的图片尺寸
    # ex_patch.extract_patches(slide, mask_img, pr.EXTRACT_SCALE, pr.PATCH_SIZE_HIGH)
    # tag = slide.release_slide_pointer()

    ex_patch = Patch()
    ex_patch.get_roi_seeds_and_save_txt(fullImage, pr.EXTRACT_PATCH_DIST,original_points_txt_path,if_write_original_txt)
    # # # 这里EXTRACT_SCALE应该是放大倍数，PATCH_SIZE_HIGH应该是截出来的图片尺寸
    ex_patch.extract_patches_and_save_txt(slide, mask_img, pr.EXTRACT_SCALE, pr.PATCH_SIZE_HIGH, result_points_txt_path, if_result_original_txt)
    # tag = slide.release_slide_pointer()


# 展示切块中各个处理时期的图片模样
def show_images_in_process(fullImage,mask_img):
    roi_img = get_roi(fullImage)
    # seeds = get_seeds(roi_img, 10)
    # patch_image = draw_seeds(fullImage, seeds, 32)
    seeds = get_seeds(roi_img, pr.EXTRACT_PATCH_DIST)  # 切块实际处理时的参数就是EXTRACT_PATCH_DIST
    patch_image = draw_seeds(fullImage, seeds, 32)  # 不知道这步是干嘛的？？？？？？？？

    fig, axes = plt.subplots(2, 2, figsize=(4, 3))
    ax = axes.ravel()
    ax[0].imshow(fullImage)
    ax[0].set_title("fullImage")
    ax[1].imshow(mask_img)
    ax[1].set_title("mask_img")
    ax[2].imshow(roi_img)
    ax[2].set_title("roi_img")
    ax[3].imshow(patch_image)
    ax[3].set_title("patch_image")
    for a in ax.ravel():
        a.axis('off')
    plt.show()

# 保存图片的方法
def save_img(image,output_image_path):
    image.save(output_image_path)

if __name__ == "__main__":
    WSI_path = 'K:/Original_Data/WSI/1705206-5 HER2_2017-07-29 08_37_30.kfb'
    WSI_number = '1705206-5'  # 与上面的WSI的编号是一样的
    annotation_path = 'K:/Original_Data/WSI/1705206-5 HER2_2017-07-29 08_37_30.kfb.Ano'
    slide, fullImage, mask_img = read_kfb(WSI_path,annotation_path,WSI_number)

    # 展示图片
    # show_images_in_process(fullImage,mask_img)

    # 保存缩略图
    # fullImage_path = '../../../data/result/image/' + WSI_number + '_HER2.jpg'
    # save_img(fullImage,fullImage_path)

    original_points_txt_path = '../../../data/txt/roi/original_roi.txt'
    result_points_txt_path = '../../../data/txt/roi/result_roi.txt'

    # 这里切块x,y貌似是左上角开始算的
    patches(slide,fullImage,mask_img,original_points_txt_path,result_points_txt_path,False,True)